<template>
    <view class="accountManage">
      <!-- 头部 -->
		<view class="header_bar header_fixed header_main">
			<view class="header">
				<view class="header-goBack" @tap.stop="$tools.back(1)"></view>
				<view class="header-title">账号管理</view>
			</view>
		</view> 
    <view class="content">
      <view class="title">当前登录账号</view>
      <view class="item">
        <view class="imgBox">
          <image :src="JSON.stringify(userInfo) != '{}' ? userInfo.avatar : ' ../../static/pack/logo.png'" mode="aspectFill"></image>
          <view class="super" v-if="userInfo.hostId==0">主</view>
        </view>
        <view class="right">
          <view class="name">{{ userInfo.nickName }}</view>
          <view class="phone">{{ userInfo.regAddress }}</view>
        </view>
      </view>
      <view class="title">其他账号</view>
      <view class="list">
        <view class="item" v-for="(item,index) in list" :key="index"
        @tap.stop="changeAccount(item)" v-if="item.regAddress !=userInfo.regAddress">
          <view class="imgBox">
            <image :src="JSON.stringify(item.avatar) != '{}' ? item.avatar : ' ../../static/pack/logo.png'" mode="aspectFill"></image>
            <view class="super" v-if="item.hostId==0">主</view>
          </view>
          <view class="right">
            <view class="name">{{ item.nickName }}</view>
            <view class="phone">{{ item.regAddress }}</view>
          </view>
        </view>
      </view>
    </view>
    <!-- <view class="submit" @tap.stop="addAccount()">新增账号</view> -->
    </view>
</template>

<script>
export default {
    data () {
      return {
        list:[],
        userInfo: {},
      };
    },
    onShow() {
     this.init()
    },
    methods: {
      init(){
        this.list = []
        this.getUserInfo();
        this.getList()
      },
      // 切换账号
      changeAccount(item){
        if(item.regAddress == this.userInfo.regAddress){
          return  this.$tools.toast('已在当前账号');
        }
        var data = {
          account:item.regAddress,
          type:1
        };
        uni.showLoading({
					mask: true
				});
        const that = this
				this.$Ajax('/api/user/loginNoPwd ',data,res => {
          uni.hideLoading();
          if (res.code == 200) {
            uni.setStorageSync('token', res.data.token);
            uni.setStorageSync('user', res.data);
            uni.removeStorageSync('address_id');
            that.$tools.toastSwitchTab('切换成功', '/pages/user/user');
          }
					}, err =>{
            uni.hideLoading();
					}
				)
      },
      // 添加账号
      addAccount(){
        if(this.userInfo.hostId==0){
          this.$tools.jump('/pages/operate/register?hostId='+this.userInfo.userId)
        }else{
          let userId=''
          this.list.forEach(item => {
            if(item.hostId==0){
              userId = item.userId
            }
          });
          if(userId){
            this.$tools.jump('/pages/operate/register?hostId='+userId)
          }
        }
      },
      getList() {
				var data = {};
				this.$Ajax('/api/user/getSonUserList ',data,res => {
						if (res.code == 200) {
							this.list = res.data;
						}
					}, err =>{
					}, 'GET'
				)
			},
    },
};
</script>

<style lang="scss" scoped>
.accountManage{
  background: #fff;
  min-height: 100vh;
  height: 100%;
  overflow: hidden;
  position: relative;
.content{
  padding:0 32rpx;
  padding-top: 90rpx;
  padding-bottom: 140rpx;
  .title{
    margin-top: 32rpx;
    font-weight: 500;
    font-size: 34rpx;
    color: rgba(0,0,0,0.9);
    line-height: 40rpx;
  }
  .item{
    margin-top: 32rpx;
    @include flexLeft;
    background: #F9F9F9;
    border-radius: 20rpx;
    padding: 38rpx;
    .imgBox{
      width: 104rpx;
      height: 104rpx;
      overflow: hidden;
      position: relative;
      image{
        width: 100%;
        height: 100%;
        border-radius: 100%;
        overflow: hidden;
      }
      .super{
        position: absolute;
        top: 0;
        left: 0;
        width: 40rpx;
        text-align: center;
        height: 40rpx;
        line-height: 40rpx;
        background: #EE3A33;
        border-radius: 100%;
        font-weight: 600;
        font-size: 20rpx;
        color: #FFFFFF;
      }
    }
    .right{
      margin-left: 40rpx;
      .name{
        font-weight: 600;
        font-size: 36rpx;
        color: #333333;
      }
      .phone{
        font-size: 28rpx;
        color: #333333;
        margin-top: 20rpx;
      }
    }
  }
}
.submit{
  width: 93.6%;
  margin: auto;
  text-align: center;
  height: 80rpx;
  line-height: 80rpx;
  background: #EE3A33;
  border-radius: 20rpx;
  font-size: 30rpx;
  color: #FFFFFF;
  position: fixed;
  bottom: 20rpx;
  left: 3.2%;
}
}
</style>
